package com.t.bricks.bussiness.db.dao.userandopenid.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.userandopenid.UserAndOpenIdDao;
import com.t.bricks.bussiness.db.entity.userandopenid.UserAndOpenId;
import com.t.bricks.bussiness.db.mapper.userandopenid.UserAndOpenIdMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.json.JacksonShell;
import com.t.bricks.utils.function.url.URLUtil;

@Repository
public class UserAndOpenIdDaoImpl implements UserAndOpenIdDao {
	
	@Autowired
	private UserAndOpenIdMapper userAndOpenIdMapper;

	/**
	 * 获取用户编号用的请求路径
	 */
	@Value("${app.userid.info.url:配置文件中的app.userid.info.url未设置}")
	private String userIdUrl;

	/**
	 * 请求'基本系统服务'用的内部密钥
	 */
	@Value("${app.BaseSystemService.sInsideKey:配置文件中的app.BaseSystemService.sInsideKey未设置}")
	private String baseSystemInsideKey;

	/**
	 * 添加
	 * @param userAndOpenId
	 * @return MsgEmity
	 */
	@Transactional
	@Override
	public MsgEmity add(UserAndOpenId userAndOpenId) {
		try {
			userAndOpenIdMapper.delByUserIdOrOpenId(//必须删除已经添加的用户或者关注号
					userAndOpenId.getsId(),
					userAndOpenId.getsOpenId());
			int i = userAndOpenIdMapper.add(userAndOpenId);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", userAndOpenId);
	}
	
	/**
	 * 根据关注号查询数据
	 * @param sOpenId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByOpenId(String sOpenId) {
		UserAndOpenId userAndOpenId = null;
		try {
			userAndOpenId = userAndOpenIdMapper.findByOpenId(sOpenId);
			if (null == userAndOpenId) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", userAndOpenId);
	}

	/**
	 * 根据用户和密码获取用户编号
	 * @param sNameOrNo 用户的名称或工号
	 * @param sPass 用户密码
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity getUserId(String sNameOrNo, String sPass) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sNameOrNo", sNameOrNo);
		map.put("sPass", sPass);
		map.put("sInsideKey", baseSystemInsideKey);
		
		MsgEmity me = URLUtil.post(userIdUrl, map);
		if (!me.isSuccess()) {
			return me;
		}
		
		me = JacksonShell.toJavaObject(me.getData().toString(), MsgEmity.class);
		return me;
	}
	
	/**
	 * 覆盖添加(先删除后添加)
	 * @param userAndOpenId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity cover(UserAndOpenId userAndOpenId) {
		try {
			int i = userAndOpenIdMapper.del(userAndOpenId.getsId());
			i = userAndOpenIdMapper.add(userAndOpenId);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", userAndOpenId);
	}

}
